﻿app.controller('PlaceSafetyController', function ($scope, $http, $window, $compile, $filter, ipCookie, common, option, Upload) {
    $scope.result = {};
    $scope.dateOption = option.date;
    $scope.datetimeOption = option.datetime;


    $scope.GetParkList = function () {
        $scope.ParkList = [];
        $http({
            method: 'POST',
            url: '../SafetyConfig/GetParkList'
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.ParkList = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.GetBuildingList = function (id) {
        $scope.BuildingList = [];
        $http({
            method: 'POST',
            url: '../SafetyConfig/GetBuildingList',
            data: { 'ParkId': id }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.BuildingList = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.GetFloorList = function (id) {
        $scope.FloorList = [];
        $http({
            method: 'POST',
            url: '../SafetyConfig/GetFloorList',
            data: { 'BuildingId': id }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.FloorList = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.GetBuildingRoomPageList = function (model, page) {
        $scope.PageLoading = true;
        $scope.RoomPageList = [];
        $http({
            method: 'POST',
            url: '../SafetyConfig/GetBuildingRoomPageList',
            data: { "model": model },
            params: { 'page': page }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.RoomPageList = ret.Data.Data;
                    $scope.TotalCount = ret.Data.TotalItemCount;
                    $scope.PageLoading = false;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.GetMyPlacePageList = function (model, page) {
        $scope.PageLoading = true;
        $scope.PlacePageList = [];
        $http({
            method: 'POST',
            url: '../SafetyConfig/GetMyPlacePageList',
            data: { "model": model },
            params: { 'page': page }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                $scope.PageLoading = false;
                if (ret.Code == 100) {
                    $scope.PlacePageList = ret.Data.Data;
                    $scope.TotalCount = ret.Data.TotalItemCount;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }




    $scope.GetDeviceList = function (model, page) {
        $scope.PageLoading = true;
        $scope.DeviceList = [];
        $http({
            method: 'POST',
            url: '../FacilityAndEquipment/GetFacilityPageList',
            data: { "model": model },
            params: { 'page': page }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.DeviceList = ret.Data.Data;
                    $scope.TotalCount = ret.Data.TotalItemCount;
                    $scope.PageLoading = false;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    //$scope.InitMyPlaceListPage = function () {
    //    mui.init({
    //        pullRefresh: {
    //            container: '#pullrefresh',
    //            down: {
    //                callback: $scope.GetMyPlaceList
    //            }
    //        }
    //    });
    //}

    $scope.GetMyPlaceList = function (model) {
        mui.showLoading("正在加载..", "div");
        $scope.PlacePageList = [];
        $http({
            method: 'POST',
            url: '../SafetyConfig/GetMyPlaceListGroupByPark',
            data: { "model": model }
        })
            .success(function (ret) {
                mui.hideLoading();
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.PlacePageList = ret.Data;
                }
                else {
                    mui.toast(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.GetDeviceInfo = function (id) {
        $scope.DataModel = {};
        $http({
            method: 'POST',
            url: '../FacilityAndEquipment/GetEquAndFac',
            data: { 'Id': id }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.DataModel = ret.Data;
                    $scope.DataModel.ParkId = $scope.DataModel.ParkId + "";
                    $scope.DataModel.CompanyId = $scope.DataModel.CompanyId + "";
                    $scope.DataModel.Status = $scope.DataModel.Status + "";
                    $scope.DataModel.DeviceType = $scope.DataModel.DeviceType + "";
                    $scope.BindDepartmentList('dep_select', $scope.DataModel.DepartmentId);
                    $scope.DataModel.CreateTime = $filter('JSONDate')($scope.DataModel.CreateTime, 'yyyy-MM-dd HH:mm:ss');
                    if ($scope.DataModel.NextMaintainceDate != null) {
                        $scope.DataModel.NextMaintainceDate = $filter('JSONDate')($scope.DataModel.NextMaintainceDate, 'yyyy-MM-dd');
                    }
                    $scope.GetPlaceList('placelist', $scope.DataModel.ParkId, $scope.DataModel.RoomId);
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.GetCurrentDevice = function (model) {
        $scope.AddModel.DeviceId = model.Id;
        $scope.AddModel.CompanyId = model.CompanyId;
        $scope.AddModel.CompanyName = model.CompanyName;
    }

    $scope.AddMaintanceRecord = function (model, searchmodel, page) {
        $http({
            method: 'POST',
            url: '../FacilityAndEquipment/AddDeviceMaintance',
            data: { 'model': model },
        })
            .success(function (ret) {
                if (ret.Code == 100) {
                    alertify.alert("维保数据更新成功");
                    if ($scope.deviceId != '') {
                        location.href = $scope.pagename;
                    }
                    else {
                        $scope.GetDeviceList(searchmodel, page);
                    }
                    $("#close").click();
                } else
                    alertify.alert(ret.Msg);
            })
            .error(function (data, status, headers, config) {
                common.error($scope);
            })
    }

    $scope.GetRiskList = function (Id) {
        $scope.DataLoading = true;
        $scope.RiskList = [];
        $http({
            method: 'POST',
            url: '../SafetyResponsibility/GetRiskListByRoomId',
            data: { "RoomId": Id }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                $scope.DataLoading = false;
                if (ret.Code == 100) {
                    $scope.TotalCount = ret.Data.length;
                    $scope.RiskList = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.GetPlaceResponsibilityAttrList = function (Id) {
        $scope.DataLoading = true;
        $scope.AttrList = [];
        $http({
            method: 'POST',
            url: '../SafetyResponsibility/GetResponsibilityAttrListByRoomId',
            data: { 'RoomId': Id }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                $scope.DataLoading = false;
                if (ret.Code == 100) {
                    $scope.AttrList = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.InitEquTask = function (id) {
        setTimeout(function () {
            if (id == null || id == '') {
                return false;
            }
            $("#m_" + id).click();
        }, 2000);
    }

});


app.filter('GetStatus', function () { //可以注入依赖
    return function (text) {
        if (text == '0')
            return "未使用";
        else if (text == '1')
            return "使用中";

    }
});

app.filter('GetBoolStr', function () { //可以注入依赖
    return function (text) {
        if (text)
            return "使用中";
        else
            return "未使用";

    }
});
app.filter('GetWeekDay', function () {
    return function (enddate) {
        var endDate = new Date(enddate);
        var beginDate = new Date(endDate.getFullYear(), 0, 1);
        //星期从0-6,0代表星期天，6代表星期六
        var endWeek = endDate.getDay();
        if (endWeek == 0) endWeek = 7;
        var beginWeek = beginDate.getDay();
        if (beginWeek == 0) beginWeek = 7;
        //计算两个日期的天数差
        var millisDiff = endDate.getTime() - beginDate.getTime();
        var dayDiff = Math.floor((millisDiff + (beginWeek - endWeek) * (24 * 60 * 60 * 1000)) / 86400000);
        return Math.ceil(dayDiff / 7) + 1;
    }

});